<?php /* CALENDAR $Id: week_view.php,v 1.27.8.1 2006/11/22 19:15:42 gregorerhardt Exp $ */
$AppUI->savePlace();
global $locale_char_set;

require_once( $AppUI->getModuleClass( 'tasks' ) );

// retrieve any state parameters
if (isset( $_REQUEST['company_id'] )) {
	$AppUI->setState( 'CalIdxCompany', intval( $_REQUEST['company_id'] ) );
}
$company_id = $AppUI->getState( 'CalIdxCompany' ) !== NULL ? $AppUI->getState( 'CalIdxCompany' ) : $AppUI->user_company;

$event_filter = $AppUI->checkPrefState('CalIdxFilter', @$_REQUEST['event_filter'], 'EVENTFILTER', 'my');

// get the passed timestamp (today if none)
$date = dPgetParam( $_GET, 'date', null );

// establish the focus 'date'
$this_week = new CDate( $date );
$dd = $this_week->getDay();
$mm = $this_week->getMonth();
$yy = $this_week->getYear();

// prepare time period for 'events'
$first_time = new CDate( Date_calc::beginOfWeek( $dd, $mm, $yy, FMT_TIMESTAMP_DATE, LOCALE_FIRST_DAY ) );
$first_time->setTime( 0, 0, 0 );
$first_time->subtractSeconds( 1 );
$last_time = new CDate( Date_calc::endOfWeek( $dd, $mm, $yy, FMT_TIMESTAMP_DATE, LOCALE_FIRST_DAY ) );
$last_time->setTime( 23, 59, 59 );

$prev_week = new CDate( Date_calc::beginOfPrevWeek( $dd, $mm, $yy, FMT_TIMESTAMP_DATE, LOCALE_FIRST_DAY ) );
$next_week = new CDate( Date_calc::beginOfNextWeek( $dd, $mm, $yy, FMT_TIMESTAMP_DATE, LOCALE_FIRST_DAY ) );

$tasks = CTask::getTasksForPeriod( $first_time, $last_time, $company_id );
$events = CEvent::getEventsForPeriod( $first_time, $last_time );

$links = array();

// assemble the links for the tasks
require_once( dPgetConfig( 'root_dir' )."/modules/calendar/links_tasks.php" );
getTaskLinks( $first_time, $last_time, $links, 50, $company_id );

// assemble the links for the events
require_once( dPgetConfig( 'root_dir' )."/modules/calendar/links_events.php" );
getEventLinks( $first_time, $last_time, $links, 50 );

// setup the title block
$titleBlock = new CTitleBlock( 'Week View' );
$titleBlock->addCell( $AppUI->_('View') . ':');
$titleBlock->addCell(
	arraySelect($event_filter_list, 'event_filter', 'onChange="document.pickFilter.submit()" class="text"',
	$event_filter, true ), '', "<Form action='{$_SERVER['REQUEST_URI']}' method='post' name='pickFilter'>", '</form>'
);
$titleBlock->addCell();
$titleBlock->addButton( 'month view', 'index.php?m=calendar&date='.$this_week->format( FMT_TIMESTAMP_DATE ) );
$titleBlock->addCell();
$titleBlock->show();
?>

<style type="text/css">
TD.weekDay  {
	height:120px;
	vertical-align: top;
	padding: 1px 4px 1px 4px;
	border-bottom: 1px solid #ccc;
	border-right: 1px solid  #ccc;
	text-align: left;
}
</style>

<table border="0" cellspacing="1" cellpadding="2" width="100%" class="minical">
<tr>
	<td>&nbsp;</td>
	<td>
		<a href="<?php echo '?m=calendar&a=week_view&date='.$prev_week->format( FMT_TIMESTAMP_DATE ); ?>"><img src="images/prev.gif" title="Previous week"></A>
	</td>
	<td width="100%" align="center">
		<b><?php echo $AppUI->_( 'Week' ).' '.htmlentities($first_time->format( "%U - %Y" ), ENT_COMPAT, $locale_char_set); ?></b>
	</td>
	<td>
		<a href="<?php echo '?m=calendar&a=week_view&date='.$next_week->format( FMT_TIMESTAMP_DATE ); ?>"><img src="images/next.gif" title="Next week"></A>
	</td>
	<td>&nbsp;</td>
</tr>
</table>
<br/>
<table border="0" cellspacing="0" cellpadding="2" width="100%" class="list">
<?php
$column = 0;
$show_day = $this_week;

$today = new CDate();
$today = $today->format( FMT_TIMESTAMP_DATE );

for ($i=0; $i < 8; $i++) {
	$dayStamp = $show_day->format( FMT_TIMESTAMP_DATE );

	$day  = $show_day->getDay();
	$href = "?m=calendar&a=day_view&date=$dayStamp&tab=0";

	$s = '';
	if ($column == 0) {
		$s .= '<tr class="second" height="120">';
	}
	$s .= '<td valign="top" width="50%" class="week_day">';

	$s .= '<table width="100%" border="0" cellpadding="0" cellspacing="0">';
	$s .= '<tr><th align="';
	$s .= ($column == 0) ? 'left" class="clickable' : 'right" class="last clickable';
	$s .= '" onclick="go(\''.$href.'\')">';

	$s .= $dayStamp == $today ? '<span style="color:white">' : '';
	$day_string = htmlentities($show_day->format("%d"), ENT_COMPAT, $locale_char_set);
	$day_name = htmlentities($show_day->format("%A"), ENT_COMPAT, $locale_char_set);
	$s .= ($column == 0) ? "$day_string $day_name" :  "$day_name $day_string";

	$s .= $dayStamp == $today ? '</span>' : '';
	$s .= '</th></tr>';

	$s .= '<tr><td class="week_day_info">';

	if (isset( $links[$dayStamp] )) {
		foreach ($links[$dayStamp] as $e) {
			$href = isset($e['href']) ? $e['href'] : null;
			$alt = isset($e['alt']) ? $e['alt'] : null;

			if ($href != '')
				$s .= '<img src="images/'. ($dayStamp == $today ? 'task_today.gif' : 'task.gif').'">&nbsp;';
			$s .= $href ? '<a href="'.$href.'" title="'.$alt.'">' : '';
			$s .= $e['text'];
			$s .= $href ? '</a><br/>' : '';
			$s .= '<br/>';
		}
	}
	else {
		$s .= '&nbsp;';
	}

	$s .= '</td></tr></table>';

	$s .= '</td>';
	if ($column == 1) {
		$s .= '</tr>';
	}
	$column = 1 - $column;

// select next day
	$show_day->addSeconds( 24*3600 );
	echo $s;
}
?>
<tr>
	<td colspan="2" align="right">
		<a href="./index.php?m=calendar&a=week_view"><?php echo $AppUI->_('today');?></A>
	</td>
</tr>
</table>
